#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Sends discovery information to Home Assistant.
# ==============================================================================
declare config

# Wait for Whisper to become available
bash -c \
    "until
        echo '{ \"type\": \"describe\" }' 
            > /dev/tcp/localhost/10300; do sleep 0.5;
    done" > /dev/null 2>&1 || true;

config=$(\
    bashio::var.json \
        uri "tcp://$(hostname):10300" \
)

if bashio::discovery "wyoming" "${config}" > /dev/null; then
    bashio::log.info "Successfully send discovery information to Home Assistant."
else
    bashio::log.error "Discovery message to Home Assistant failed!"
fi
